import React from 'react'
import {Link,browserHistory} from 'react-router'

import '../public/css/common.css'
import '../public/css/nav.scss'

import head from '../public/img/img_head.png'
import requestSign from "../public/js/requestSign";
import Api from "../router/Api";

const navLists = [
    {
        text:'我的资产',
        link:'/',
        classname:"icon-ic_myassets icon iconfont"
    },{
        text:"我的借款",
        link:"/myloan",
        classname:"icon iconfont icon-ic_myloan"
    },{
        text:"账户信息",
        link:'/account',
        classname:"icon iconfont icon-ic_myaccount"
    }
]
const lists = navLists.map((navlist,i)=>
    <li key={i}><Link to={navlist.link} activeClassName="active"><i className={navlist.classname}></i>{navlist.text}</Link></li>
);
class Nav extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            name:""
        }
    }
    componentWillMount(){
        if(!sessionStorage.getItem('token')){
            browserHistory.push('/login')
        }else{
            if(sessionStorage.getItem('isActivation') === '0'){
                browserHistory.push('/activation')
            }
        }
        const token = sessionStorage.getItem('token');
        const _this = this;
        Api.getTimeStamp()
            .then(function (res) {
                const timestamp = res.data.data.timestamp;
                const sign = requestSign({timestamp,token});
                Api.getBorrowUserInfo({
                    token:token,
                    timestamp:timestamp,
                    sign:sign.sign
                })
                    .then(function (res) {
                        if(res.data.data.status === 200){
                            browserHistory.push('/login')
                        }
                        if(res.data.data.userType === 0){
                            _this.setState({
                                name:res.data.data.realName
                            })
                        }else{
                            _this.setState({
                                name:res.data.data.realName
                            })
                        }

                    })
            })
            .catch(function (err) {
                console.log(err);
            })
    }
    handleShow(){
        this.props.dialogShow("true")
    }
    render(){
        return(
            <div className="nav">
                <img src={head} alt=""/>
                <p style={{height:"19"}}>{this.state.name}</p>
                <div className="clearFix">
                    <div className="fl">已实名</div>
                    <div className="fr">已绑卡</div>
                </div>
                <ul>{lists}</ul>
                <div className="loginout" onClick={this.handleShow.bind(this)}><i className="icon iconfont icon-ic_quit"></i>退出登录</div>
            </div>
        )
    }
}


export default Nav